package com.example.mam.web;


import com.example.mam.dto.BaseResult;
import com.example.mam.entity.Investor;
import com.example.mam.entity.Master;
import com.example.mam.exception.BizException;
import com.example.mam.exception.BizExceptionEnum;
import com.example.mam.exception.BizExceptionMessage;
import com.example.mam.service.InvestorService;
import com.example.mam.service.MasterService;
import com.example.mam.util.ExceptionUtils;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

@RestController
@RequestMapping("/master")
public class MasterController {

    @Resource
    private MasterService masterService;
    @Resource
    private InvestorService investorService;


    /**
     * 绑定主账号
     *
     * @param master 主账号信息
     * @return
     */
    @GetMapping("/bindMasterAccount")
    public BaseResult<Object> bindMasterAccount(Master master) {

        master.setLogin(1003L);
        masterService.insertMaster(master);
        return new BaseResult<>(true, null);
    }


    /**
     * 给某个主账号绑定投资者
     *
     * @param investor 传两个参数： 投资者Login    主账号Login
     * @return
     */
    @GetMapping("/bindInvestorAccount")
    public BaseResult<Object> bindInvestorAccount(Investor investor) {
        investor.setLogin(2002L);
        investor.setMasterLogin(2001L);
        try {
            investorService.insertInvestor(investor);
        } catch (BizException e) {
            if (e.getBizExceptionEnum().getCode() == BizExceptionEnum.NLESSINITIALINVESTMENTS_ERROR.getCode())
                return new BaseResult<>(false, new BizExceptionMessage(BizExceptionEnum.NLESSINITIALINVESTMENTS_ERROR));
            if (e.getBizExceptionEnum().getCode() == BizExceptionEnum.NGROUPMASTERINVALID_ERROR.getCode())
                return new BaseResult<>(false, new BizExceptionMessage(BizExceptionEnum.NGROUPMASTERINVALID_ERROR));

        }
        return new BaseResult<>(true, null);
    }


    /**
     * 给某个主账号绑定投资者
     *
     * @param investor 传两个参数： 投资者Login    主账号Login
     * @return
     */
    @GetMapping("/bindMFAccount")
    public BaseResult<Object> bindMFAccount(Investor investor){
        investor.setLogin(2002L);
        investor.setMasterLogin(2001L);
        return new BaseResult<>(true, null);
    }


}
